#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("欢迎界面");

    //连接数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");  //连接本地主机
    db.setPort(3306);
    db.setDatabaseName("exp");
    db.setUserName("root");
    db.setPassword("123456");
    bool ok = db.open();
    if (ok){
        QMessageBox::information(this, "信息", "数据库连接成功！");
    }
    else {
        QMessageBox::information(this, "信息", "数据库连接失败！");
        qDebug()<<"error open database because"<<db.lastError().text();
    }

    //设置界面logo
    QPixmap logo(":/res/logo.png");
    logo=logo.scaled(ui->label_logo->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    ui->label_logo->setPixmap(logo);

    //创建登录窗口
    loginDialog = new LoginDialog(this);

    //登录成功后设置界面
    connect(loginDialog,&LoginDialog::doctorLoginSuccess,[=]{
        this->setWindowTitle("医生窗口");
        DoctorWindow *dw=new DoctorWindow(this);
        ui->stackedWidget->addWidget(dw);
        ui->stackedWidget->setCurrentWidget(dw);
    });
    connect(loginDialog,&LoginDialog::patientLoginSuccess,[=]{
        this->setWindowTitle("患者窗口");
        PatientWindow *pw=new PatientWindow(this);
        ui->stackedWidget->addWidget(pw);
        ui->stackedWidget->setCurrentWidget(pw);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionQuit_triggered()
{
    this->close();
}

void MainWindow::on_toolButton_patientLogin_clicked()
{
    //调出患者登录界面
    loginDialog->setCurrentPage(patient);
    loginDialog->exec();
}

void MainWindow::on_toolButton_doctorLogin_clicked()
{
    //调出医生登录界面
    loginDialog->setCurrentPage(doctor);
    loginDialog->exec();
}
